import os
os.environ["QT_API"] = "pyqt5"
from PyUIC.AGI_PaintWindows import Ui_Paint_Widget
from AGI_Widgets.QPaintWidgetView import QPaintWidgetView
from PyQt5.QtWidgets import QWidget,QTableWidgetItem,QMenu,QVBoxLayout,QAction
import pyvista as pv
from pyvistaqt import QtInteractor

import cv2
from QSS.load_qss import QSSLoader
class AGIPaintWidget(QWidget,Ui_Paint_Widget):
    def __init__(self,cfg,parent = None):
        super(AGIPaintWidget,self).__init__(parent)
        self.setupUi(self)
        self.cfg = cfg
        tab_idx = self.cfg['UI_State']

        self.tabWidget.setCurrentIndex(int(tab_idx))
        self.PaintWidget2D = QPaintWidgetView()
        self.PaintWidgetAI = QPaintWidgetView()
        self.PaintWidgetAOI_3D = QPaintWidgetView()
        self.PaintWidgetAOI_RES = QPaintWidgetView()
        self.PaintWidgetAOI_AC = QPaintWidgetView()
        self.PaintWidgetAOI_Img = QPaintWidgetView()

        self.gridLayout_2d.addWidget(self.PaintWidget2D)
        self.gridLayout_ai.addWidget(self.PaintWidgetAI)
        # self.gridLayout_aoi_3d.addWidget(self.PaintWidgetAOI_3D)
        self.gridLayout_aoi_img.addWidget(self.PaintWidgetAOI_Img)
        self.gridLayout_aoi_ac.addWidget(self.PaintWidgetAOI_AC)
        self.gridLayout_aoi_res.addWidget(self.PaintWidgetAOI_RES)
        self.rect = []

        # create the frame
        # add the pyvista interactor object
        self.plotter = QtInteractor()
        # self.plotter.
        # self.plotter.interactor.
        self.gridLayout_aoi_3d.addWidget(self.plotter.interactor)
        # self.groupBox_3.adjustSize(200, 200)
        # self.groupBox_4.setSizeIncrement(200, 200)
        # self.groupBox_5.setSizeIncrement(200, 200)
        # self.groupBox_6.setSizeIncrement(200, 200)

    def paint_3d_image(self,images):
        heatmap = images[0]
        texture = images[1]
        terrain = heatmap.warp_by_scalar(factor=1)
        terrain.texture_map_to_plane(use_bounds=False, inplace=True)
        self.plotter.clear()
        self.plotter.add_mesh(terrain, texture=texture, show_edges=False)
        self.plotter.reset_camera()
        # self.gridLayout_aoi_3d.
        # self.signal_close.connect(self.plotter.close)
        # self.frame.setLayout(self.gridLayout_aoi_3d)
        # image = pv.read(r'F:\Benchuang\AI_External-20230817122341\20230817122341\NGPartImage\1@1.ptt')
        # tex = pv.read_texture(r'D:\WJY\ptt\jpg\1@289.jpg')
        # terrain = image.warp_by_scalar(factor=1 / 20)
        # terrain.texture_map_to_plane(use_bounds=False, inplace=True)

        # mpl.rcParams['figure.dpi'] = 500
        # bounds = image.bounds
        # # Clip the elevation dataset to the map's extent
        # local = terrain.clip_box(bounds, invert=False)
        # # Apply texturing coordinates to associate the image to the surface
        # local.texture_map_to_plane(use_bounds=True, inplace=True)
        #
        # local.plot(texture=tex, cpos="xy")
        # self.plotter.add_mesh(terrain,texture=tex, show_edges=False)
        # self.plotter.te
        # self.plotter.a
        # self.plotter.reset_camera()
        # allow adding a sphere
        # mainMenu =QMenu()
        # meshMenu = mainMenu.addMenu('Mesh')
        # self.add_sphere_action = QAction('Add Sphere', self)
        # self.add_sphere_action.triggered.connect(self.add_sphere)
        # meshMenu.addAction(self.add_sphere_action)

    # def add_sphere(self):
    #     """ add a sphere to the pyqt frame """
    #     sphere = pv.Sphere()
    #     self.plotter.add_mesh(sphere, show_edges=True)
    #     self.plotter.reset_camera()

    # def display(self,str):
    #     self.listWidget.addItem(str)
    def paint_image(self,images):
        assert len(images) == 2 ,'Error images!'
        self.PaintWidget2D.open_image(images[0])
        if len(self.rect)>0:
            self.PaintWidget2D.setRect(self.rect)
        self.PaintWidgetAI.open_image(images[1])
    def paint_aoi_image(self,images):
        # self.PaintWidgetAOI_3D
        assert len(images) == 3,'Error images!'
        self.PaintWidgetAOI_Img.open_image(images[0])
        if len(self.rect)>0:
            self.PaintWidgetAOI_Img.setRect(self.rect)
        self.PaintWidgetAOI_AC.open_image(images[1])
        self.PaintWidgetAOI_RES.open_image(images[2])

        pass

    def paint_2d(self,image):
        pass

    def paint_ai(self,image):
        pass

    def height_slider(self):
        pass

    def area_slider(self):
        pass

    def write_aoi_tablewidget(self,args):

        curr_row = self.aoi_tablewidget.rowCount()
        self.aoi_tablewidget.insertRow(curr_row)
        self.aoi_tablewidget.setItem(curr_row, 0, QTableWidgetItem(args[0]))
        self.aoi_tablewidget.setItem(curr_row, 1, QTableWidgetItem(args[1]))
        self.aoi_tablewidget.setItem(curr_row, 2, QTableWidgetItem(args [2]))
        # self.repaint()
        self.aoi_tablewidget.selectRow(curr_row)
    def write_ai_tablewidget(self,args):

        curr_row = self.ai_tablewidget.rowCount()
        self.ai_tablewidget.insertRow(curr_row)
        self.ai_tablewidget.setItem(curr_row, 0, QTableWidgetItem(args[0]))
        self.ai_tablewidget.setItem(curr_row, 1, QTableWidgetItem(args[1]))
        self.ai_tablewidget.setItem(curr_row, 2, QTableWidgetItem(args [2]))
        # self.repaint()
        self.ai_tablewidget.selectRow(curr_row)

    def write_roi_group(self,args):
        self.X1_lineEdit.setText(str(args[2]))
        self.Y1_lineEdit.setText(str(args[3]))
        self.X2_lineEdit.setText(str(args[4]))
        self.Y2_lineEdit.setText(str(args[5]))
        self.L_lineEdit.setText(str(args[1]))
        self.W_lineEdit.setText(str(args[0]))
        self.rect = args[2:]
    def clear_tablewidget(self):
        try:
            self.aoi_tablewidget.setRowCount(0)
            self.aoi_tablewidget.clearContents()
            self.ai_tablewidget.setRowCount(0)
            self.ai_tablewidget.clearContents()
            # self.aoi_tablewidget.clearContents()
            # self.aoi_tablewidget.ro
        except Exception as e:
            print(e)